package com.lyq.leetcode.盛最多水的容器11;

public class Main {

    public static void main(String[] args) {
        Main main = new Main();
        System.out.println(main.maxArea1(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}));
    }

    /**
     * 暴力解法
     *
     * @param height 盛水数组
     * @return 最大水量
     */
    public int maxArea(int[] height) {
        int res = -1;
        int maxHeight = -1;
        for (int i = 0; i < height.length - 1; i++) {
            if (height[i] < maxHeight) {
                continue;
            } else {
                maxHeight = height[i];
            }
            for (int j = i + 1; j < height.length; j++) {
                res = Math.max(res, (j - i) * Math.min(height[i], height[j]));
            }
        }
        return res;
    }

    /**
     * 双指针
     *
     * @param height 盛水数组
     * @return 最大水量
     */
    public int maxArea1(int[] height) {
        int res = -1;
        int left = 0;
        int right = height.length - 1;
        while (left < right) {
            res = Math.max(res, (right - left) * Math.min(height[right], height[left]));
            if (height[right] < height[left]) {
                right--;
            } else {
                left++;
            }
        }
        return res;
    }
}
